package com.tiger.cloud.controller.crm;

import com.tiger.cloud.common.mapper.JsonMapper;
import com.tiger.cloud.common.mybatis.BaseController;
import com.tiger.cloud.enums.DeleteEnum;
import com.tiger.cloud.filter.CurrentUserId;
import com.tiger.cloud.model.crm.Commucation;
import com.tiger.cloud.model.crm.CrmBanner;
import com.tiger.cloud.service.crm.CrmBannerService;
import com.tiger.cloud.util.ErrorCode;
import com.tiger.cloud.util.Page;
import com.tiger.cloud.util.Response;
import com.tiger.cloud.util.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping(value = "banner")
@Api(value = "APP轮播图",tags = "APP轮播图")
public class CrmBannerController extends BaseController {
    @Autowired
    private CrmBannerService crmBannerService;

    @RequestMapping(value = "findCrmBanner", method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value = "获取轮播图",notes = "获取轮播图")
    public Response findCrmBanner(@RequestBody CrmBanner crmBanner){
        crmBanner.setOrderBy("image_order asc");
        Page<CrmBanner> data = crmBannerService.page(crmBanner);
        return ResponseUtil.ok(data);
    }

    @RequestMapping(value = "insertBanner", method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value = "添加轮播图",notes = "添加轮播图")
    public Response insertCrmBanner(@RequestBody CrmBanner crmBanner){
        if(crmBannerService.insert(crmBanner)){
            return ResponseUtil.ok();
        }
        return ResponseUtil.error();
    }
    @RequestMapping(value = "getById", method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value = "根据ID获取轮播图",notes = "根据ID获取轮播图")
    public Response getById(@RequestBody CrmBanner crmBanner){
        if(crmBanner.getId() == null){
            return ResponseUtil.error("数据ID不能为空");
        }
        crmBanner = crmBannerService.get(crmBanner.getId());
        if(crmBanner == null){
            return ResponseUtil.error("没有查询到数据");
        }
        return ResponseUtil.ok(crmBanner);
    }

    @RequestMapping(value = "updateBanner", method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value = "更新轮播图",notes = "更新轮播图")
    public Response updateBanner(@RequestBody CrmBanner crmBanner){
        if(crmBanner.getId() == null){
           return ResponseUtil.error("数据ID不能为空");
        }
        if(StringUtils.isEmpty(crmBanner.getImageUrl())){
            return ResponseUtil.error("图片存放URL不能为空");
        }
        if(StringUtils.isEmpty(crmBanner.getImageContent())){
            return ResponseUtil.error("内容不能为空");
        }
        if(StringUtils.isEmpty(crmBanner.getRedirectUrl())){
            return ResponseUtil.error("图片跳转URL不能为空");
        }
        if(crmBanner.getImageOrder() == null){
            return ResponseUtil.error("轮播顺序不能为空");
        }
        if(crmBannerService.update(crmBanner) > 0){
            return ResponseUtil.ok();
        }
        return ResponseUtil.error("参数错误,请重试!");
    }

    @RequestMapping(value = "deleteCrmBanner", method = RequestMethod.POST)
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value = "删除轮播图片",notes = "删除轮播图片")
    public Response delete(@RequestBody CrmBanner crmBanner){
        if(crmBanner.getId() != null && crmBanner.getId() > 0){
            CrmBanner deleteEntity=crmBannerService.get(crmBanner.getId());
            if(deleteEntity == null){
                return ResponseUtil.error("参数错误,请重试!");
            }
            if(crmBannerService.remove(crmBanner) > 0){
                return ResponseUtil.ok();
            }
        }
        return ResponseUtil.error("参数错误,请重试!");
    }
}
